package net.xprogrammer.booclipse.generator.toc.parser.chm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import net.xprogrammer.booclipse.generator.toc.Topic;
import net.xprogrammer.booclipse.generator.toc.parser.Parser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * chm格式的toc解析器，oreilly官方发布的chm格式书籍采用这个解析器，应该适用于所有的chm文件，因为hhc是微软的格式
 * 
 * @author gyzhang
 * @date 2012-4-2 下午10:27:27
 * @version 3.0
 */
public class ChmTocParser implements Parser {

	private static final Logger	log	= LoggerFactory.getLogger(ChmTocParser.class);

	public static void main(String[] args) {
		File in = new File("D:" + File.separator + "xhtmTDG6.hhc");
		File out = new File("d:" + File.separator + "booclipse.xml");
		String hrefPrefix = "html/booclipse/web/xhtmlTDG6/";//eclipse中插件项目存放html文件的目录

		ChmTocParser parser = new ChmTocParser();
		try {
			parser.writeTOC(in, out, hrefPrefix);
		} catch (IOException e) {
			log.error(e.getMessage());
		}

	}

	@SuppressWarnings("unchecked")
	public Topic parse(File file) throws IOException {
		//一个书签的完整格式
		//<LI><OBJECT type="text/sitemap">
		//          <param name="Name" value="Section 1.1.  The Internet">
		//          <param name="Local" value="0596527322/html6-CHP-1-SECT-1.html">
		//          <param name="ImageNumber" value="11">
		//        </OBJECT>

		String siteMap = "<OBJECT type=\"text/sitemap\">";

		BufferedReader input = new BufferedReader(new FileReader(file));
		String line = null;
		String label = null;
		String href = null;
		int level = 0;

		Topic parent = new Topic("booclipse起始根节点", "", level, null);//构造的一个根节点
		Topic current = parent;//当前节点
		Topic result = parent;//最后返回的节点

		while ((line = input.readLine()) != null) {
			//遇到<UL>开始下一级
			//遇到</UL>结束本级
			if (line.indexOf("<UL>") > 1) {//开始下一级
				level++;
				parent = current;
			}
			if (line.indexOf("</UL>") > 1) {//回到上一级
				level--;
				parent = parent.getParent();
			}

			if (line.indexOf(siteMap) > 0) {//找到一个sitemap书签
				line = input.readLine();//读取Name这一行
				label = line.substring(line.indexOf("value=\"") + 7, line.indexOf("\">"));
				line = input.readLine();//读取Local这一行
				href = line.substring(line.indexOf("value=\"") + 7, line.indexOf("\">"));

				current = new Topic(label, href, level, parent);//当前节点

				parent.addSubTopic(current);//将当前topic加入到上级节点中
			}

		}

		return result;
	}

	/**
	 * 根据给定的hhc文件，解析后将最终的eclipse help toc写入到文件中
	 * 
	 * @param in
	 *            hhc文件
	 * @param out
	 *            eclipse help toc文件
	 * @param hrefPrefix
	 *            eclipse中插件项目存放html文件的目录
	 * @throws IOException
	 */
	public void writeTOC(File in, File out, String hrefPrefix) throws IOException {
		ChmTocParser parser = new ChmTocParser();
		Topic topic = null;
		FileWriter fileWriter = null;

		try {
			topic = parser.parse(in);
			fileWriter = new FileWriter(out);

			if (null != topic) {
				topic.write2Booclipse(topic, hrefPrefix, fileWriter, 0);
			}

		} finally {
			if (null != fileWriter) {
				fileWriter.close();
			}
		}
	}

}
